<?php
class Tourism_goods_category_spec_model extends BZ_Model
{
    protected $table  = 'tourism_goods_category_spec';
    protected $table_2  = 'tourism_goods_category_attribute';
    
    /*修改中*/
    public function addAttributeSpec($cid,$postData)
    {
        $data_attribute = array(
            'attr_name'=>$postData['attr_name'],
            'category_id' =>$cid
        );
        $param = array();
        $attribute_value = $this->processing($postData['attribute_value']);
        //判断是否是array
        if(!is_array($data_attribute))
        {
            return false;
        }
        $num = $this->db->get_where($this->table_2,$data_attribute)->num_rows();
        if($num)
        {
            return false;
        }
        //事务开始
        $this->db->trans_begin();
        $this->db->insert($this->table_2,$data_attribute);
        $id = $this->db->insert_id();
        foreach($attribute_value as $key =>$val)
        {
            $param[$key]['attr_id'] = $id;
            $param[$key]['attribute_value'] = $val;
        }
        if(empty($param))
        {
            $this->db->trans_rollback();
            return false;
        }
        $this->db->insert_batch($this->table,$param);
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return false;
        }
        else
        {
            $this->db->trans_commit();
            return true;
        }
    }
    /*
     * 数据处理
     * */
    private  function processing($data)
    {
        $param = str_replace('，',',',$data);
        $param = trim($param,',');
        return explode(',',$param);
    }

    /**
     * 根据attr_id 获取 tourism_goods_category_spec中的对应信息
     * @param int
     * @return object
     */
    public function getSpecByAttId($attr_id)
    {
        $this->db->where('attr_id',$attr_id);
        return $this->db->get($this->table);
    }

    /**
     * 根据attr_id 一组重现排列的字符串，用，分割
     * @param int
     * @return string
     */
    public function getSpecStringByAttId($attr_id)
    {
        $resultObj = $this->getSpecByAttId($attr_id);
        foreach($resultObj->result() as $item)
        {
            $lists[] = $item->attribute_value;
        }
        return implode(',',$lists);
    }
    /**
     * 根据attr_id 更新数据
     * @param int
     */
    public function updateAttributeSpec($attr_id,$postData){

        $data_attribute = array(
            'attr_name'=>$postData['attr_name'],
        );
        $param = array();

        $this->db->trans_begin();
        //attribute 表更新
        $this->db->where('attr_id',$attr_id);
        $this->db->update($this->table_2,$data_attribute);
        //spec 表删除
        $this->db->where('attr_id',$attr_id);
        $this->db->delete($this->table);
        //
        $attribute_value = $this->processing($postData['attribute_value']);
        foreach($attribute_value as $key =>$val)
        {
            $param[$key]['attr_id'] = $attr_id;
            $param[$key]['attribute_value'] = $val;
        }
        if(empty($param))
        {
            $this->db->trans_rollback();
            return false;
        }
        $this->db->insert_batch($this->table,$param);
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return false;
        }
        else
        {
            $this->db->trans_commit();
            return true;
        }

    }
    public function deleteSpec($attr_id)
    {
        $this->db->where('attr_id',$attr_id);
        return $this->db->delete($this->table);
    }
    /*----------------------------------------*/



    public function getSpec($attr_id){
        $this->db->select('a.attr_name,s.attr_id,s.spec_id,s.attribute_value');
        $this->db->from($this->table_2.' as a');
        $this->db->where('s.attr_id',$attr_id);
        $this->db->join($this->table.' as s ','a.attr_id = s.attr_id');
        return $this->db->get();
    }

    public function updateSpec($spec_id,$param){
        $this->db->where('spec_id', $spec_id);
        return $this->db->update($this->table, $param);
        $this->db->update();

    }
    public function addSpec($attr_id,$attribute_value){
        if(!is_array($attribute_value)){
            $attribute_value = explode(',',$attribute_value);
        }

        foreach($attribute_value as $val){
            $param[]['attr_id'] = $attr_id;
            $param[]['attribute_value'] = $val;
            $this->db->insert_batch($this->table,$param);
        }
    }

    public  function getSpecNameById($spec_id){
        $this->db->where('spec_id',$spec_id);
        $result = $this->db->get($this->table)->row_array();
        return isset($result['attribute_value'])?$result['attribute_value']:'';


    }

}